package manager

import (
	"strconv"

	"gitcode.com/G-YT/baoyou/server/global"
	"gitcode.com/G-YT/baoyou/server/model/common/response"
	mgrReq "gitcode.com/G-YT/baoyou/server/model/manager/request"
	"gitcode.com/G-YT/baoyou/server/utils"
	"github.com/gin-gonic/gin"
	"go.uber.org/zap"
)

type MessageNoticeApi struct{}

// CreateNotice 健管师创建消息提醒
// @Tags     Manager
// @Summary  健管师创建消息提醒
// @Security ApiKeyAuth
// @accept   application/json
// @Produce  application/json
// @Param    data body mgrReq.CreateNoticeReq true "用户ID与消息内容"
// @Success  200 {object} response.Response{msg=string}
// @Router   /manager/messageNotice/create [post]
func (a *MessageNoticeApi) CreateNotice(c *gin.Context) {
	hmID := utils.GetHmID(c)
	var req mgrReq.CreateNoticeReq
	if err := c.ShouldBindJSON(&req); err != nil {
		response.FailWithMessage(err.Error(), c)
		return
	}
	if err := MessageNoticeService.CreateNotice(hmID, req); err != nil {
		global.GVA_LOG.Error("创建消息提醒失败", zap.Error(err))
		response.FailWithMessage(err.Error(), c)
		return
	}
	response.OkWithMessage("创建成功", c)
}

// GetTodayStatus 用户获取当天消息提醒状态
// @Tags     Manager
// @Summary  用户获取当天消息提醒状态（0存在未读，1不存在未读）
// @Security ApiKeyAuth
// @accept   application/json
// @Produce  application/json
// @Success  200 {object} response.Response{data=mgrResp.TodayStatusResp}
// @Router   /manager/messageNotice/todayStatus [get]
func (a *MessageNoticeApi) GetTodayStatus(c *gin.Context) {
	userID := utils.GetUserID(c)
	status, err := MessageNoticeService.GetTodayUnreadStatus(userID)
	if err != nil {
		global.GVA_LOG.Error("获取消息提醒状态失败", zap.Error(err))
		response.FailWithMessage(err.Error(), c)
		return
	}
	response.OkWithData(status, c)
}

// GetWeekList 用户获取一周消息提醒列表
// @Tags     Manager
// @Summary  用户获取一周消息提醒列表（含当天，当天置已读）
// @Security ApiKeyAuth
// @accept   application/json
// @Produce  application/json
// @Success  200 {object} response.Response{data=mgrResp.MessageListResp}
// @Router   /manager/getWeekList [get]
func (a *MessageNoticeApi) GetWeekList(c *gin.Context) {
	userID := utils.GetUserID(c)
	uidStr := c.Query("user_id")
	if uidStr != "" {
		if u64, err := strconv.ParseUint(uidStr, 10, 64); err == nil && u64 > 0 {
			userID = uint(u64)
		}
	}
	resp, err := MessageNoticeService.GetMessageList(userID)
	if err != nil {
		global.GVA_LOG.Error("获取消息提醒列表失败", zap.Error(err))
		response.FailWithMessage(err.Error(), c)
		return
	}
	response.OkWithData(resp, c)
}
